package com.wanlian.dao.impl;

import com.wanlian.dao.QuestionTypeDao;
import com.wanlian.entity.QuestionType;
import com.wanlian.model.ResultModel;
import com.wanlian.util.StringUtil;
import com.wanlian.vo.Page;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class QuestionsTypeDaoImpl extends GenericDaoImpl<QuestionType> implements QuestionTypeDao {
    public QuestionsTypeDaoImpl() {
        super(QuestionType.class);
    }

    @Override
    public ResultModel queryMessage(Page page, Map<String, Object> conditions) {
            StringBuilder sqlWhere = new StringBuilder(" where 1=1 ");
            ArrayList<Object> param = new ArrayList<Object>();
            if (conditions != null) {
                if (conditions.containsKey("modelName")) {
                    String modelName = (String) conditions.get("modelName");
                    if (StringUtil.hasValue(modelName)) {
                        sqlWhere.append(" and mname like ?");
                        param.add("%" + modelName + "%");
                    }
                }
                if (conditions.containsKey("id")) {
                    int id = (int) conditions.get("id");
                        sqlWhere.append(" and id=?");
                        param.add(id);
                }


            }
            List<QuestionType> lists = new ArrayList<QuestionType>();
            String hql = String.format("from %s " + sqlWhere, entityClassName());
            lists = this.queryForListByPage(hql, param.toArray(), page);
            String countSql = String.format("select count(*) from %s" + sqlWhere, entityClassName());
            long count = this.queryCount(countSql, param.toArray());
            ResultModel resultModel = new ResultModel(true, lists, count);
            return resultModel;
    }

    @Override
    public ResultModel queryQuestionType(Page page, Map<String, Object> conditions) {
        return null;
    }


    @Override
    public ResultModel queryQuestionTypeById(int id) {
        StringBuilder sqlWhere = new StringBuilder(" where 1=1 ");
        ArrayList<Object> param = new ArrayList<Object>();
                sqlWhere.append(" and id=?");
                param.add(id);
        List<QuestionType> lists = new ArrayList<QuestionType>();
        String hql = String.format("from %s " + sqlWhere, entityClassName());
        QuestionType questionType = this.queryById(id);
        String countSql = String.format("select count(*) from %s" + sqlWhere, entityClassName());
        long count = this.queryCount(countSql, param.toArray());
        ResultModel resultModel = new ResultModel(true, questionType);
        return resultModel;
    }

    @Override
    public long  queryQuestionTypeCountById(int id) {
        StringBuilder sqlWhere = new StringBuilder(" where 1=1 ");
        ArrayList<Object> param = new ArrayList<Object>();
        sqlWhere.append(" and id=?");
        param.add(id);
        List<QuestionType> lists = new ArrayList<QuestionType>();
        String hql = String.format("from %s " + sqlWhere, entityClassName());
        QuestionType questionType = this.queryById(id);
        String countSql = String.format("select count(*) from %s" + sqlWhere, entityClassName());
        long count = this.queryCount(countSql, param.toArray());
        return count;
    }


}
